/*
 * $Id$
 *
 * Copyright ( c ) 2014 Carefx Corporation. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of Carefx
 * Corporation ("Confidential Information").  You shall not disclose such
 * Confidential Information and shall use it only in accordance with the terms
 * of the license agreement you entered into with Carefx Corporation or a Carefx
 * authorized reseller (the "License Agreement"). Carefx may make changes to the
 * Confidential Information from time to time. Such Confidential Information may
 * contain errors.
 *
 * EXCEPT AS EXPLICITLY SET FORTH IN THE LICENSE AGREEMENT, CAREFX DISCLAIMS ALL
 * WARRANTIES, COVENANTS, REPRESENTATIONS, INDEMNITIES, AND GUARANTEES WITH
 * RESPECT TO SOFTWARE AND DOCUMENTATION, WHETHER EXPRESS OR IMPLIED, WRITTEN OR
 * ORAL, STATUTORY OR OTHERWISE INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A
 * PARTICULAR PURPOSE. CAREFX DOES NOT WARRANT THAT END USER'S USE OF THE
 * SOFTWARE WILL BE UNINTERRUPTED, ERROR FREE OR SECURE.
 *
 * CAREFX SHALL NOT BE LIABLE TO END USER, OR ANY OTHER PERSON, CORPORATION OR
 * ENTITY FOR INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL
 * DAMAGES, OR DAMAGES FOR LOSS OF PROFITS, REVENUE, DATA OR USE, WHETHER IN AN
 * ACTION IN CONTRACT, TORT OR OTHERWISE, EVEN IF CAREFX HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES. CAREFX' TOTAL LIABILITY TO END USER SHALL NOT
 * EXCEED THE AMOUNTS PAID FOR THE CAREFX SOFTWARE BY END USER DURING THE PRIOR
 * TWELVE (12) MONTHS FROM THE DATE IN WHICH THE CLAIM AROSE.  BECAUSE SOME
 * STATES OR JURISDICTIONS DO NOT ALLOW LIMITATION OR EXCLUSION OF CONSEQUENTIAL
 * OR INCIDENTAL DAMAGES, THE ABOVE LIMITATION MAY NOT APPLY TO END USER.
 *
 * Copyright version 2.0
 */
package com.th.supcom.pds.prescstatusquery.client.widget;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.grid.GridSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.th.supcom.pds.common.client.commom.PdsClientDateUtils;
import com.th.supcom.pds.common.client.model.CodeConstants;
import com.th.supcom.pds.common.client.model.PdsSupplyProModel;
import com.th.supcom.pds.common.client.widget.PdsGrid;
import com.th.supcom.pds.common.enums.CheckFlag;
import com.th.supcom.pds.common.enums.WithdrawalDrugState;

/**
 * 处方状态查询处方Grid面板
 * 
 * @author 杨冲
 */
public class SupplyStatusQueryPrescGrid extends LayoutContainer
{

    private PdsGrid <PdsSupplyProModel> m_grid;

    /**
     * 构造函数
     */
    public SupplyStatusQueryPrescGrid ()
    {
        initUI ();
    }

    /**
     * 初始化UI
     */
    private void initUI ()
    {
        this.setLayout (new FitLayout ());
        this.setBorders (true);
        createGrid ();
        m_grid.getGrid ().setSelectionModel (new GridSelectionModel <PdsSupplyProModel> ());
    }

    private void createGrid ()
    {
        m_grid = new PdsGrid <PdsSupplyProModel> ()
        {

            @Override
            public ColumnModel createColumnModel ()
            {
                return getColumnModel ();
            }
        };
        m_grid.getGrid ().setColumnLines (true);
        m_grid.getGrid ().getView ().setAutoFill (true);
        add (m_grid);
    }

    /**
     * 创建Grid列信息
     * 
     * @return
     */
    private ColumnModel getColumnModel ()
    {
        List <ColumnConfig> configs = new ArrayList <ColumnConfig> ();
        ColumnConfig column = null;
        RowNumberer rowNumberer = new RowNumberer ();
        rowNumberer.setHeader ("序号");
        rowNumberer.setWidth (40);
        configs.add (rowNumberer);
        column = new ColumnConfig ("prescNo", "处方号", 100);
        configs.add (column);
        column = new ColumnConfig ("patiName", "姓名", 60);
        configs.add (column);
        column = new ColumnConfig ("rcptNo", "计价状态", 60);
        column.setRenderer (createChargeIndicatorRenderer ());
        configs.add (column);
        column = new ColumnConfig ("drugReturnTime", "退药状态", 60);
        column.setRenderer (createWithdrawalDrugStateRenderer ());
        configs.add (column);
        column = new ColumnConfig ("checkFlag", "是否审核", 60);
        column.setRenderer (createNeedAuditRenderer ());
        configs.add (column);
        column = new ColumnConfig ("qualifiedType", "审核结果", 60);
        column.setRenderer (createQualifiedIndicatorRenderer ());
        configs.add (column);
        column = new ColumnConfig ("prescStatusName", "供药状态", 60);
        column.setRenderer (createPrescSupplyDrugStateRenderer ());
        configs.add (column);
        column = new ColumnConfig ("chargeDate", "收方时间", 150);
        column.setDateTimeFormat (DateTimeFormat.getFormat (PdsClientDateUtils.DATE_FORMAT_YYYY_MM_DD_HH_MM_SS));
        configs.add (column);
        column = new ColumnConfig ("prescClassifcationName", "处方类别", 60);
        configs.add (column);
        column = new ColumnConfig ("orgIdExecName", "供药药房", 80);
        configs.add (column);
        column = new ColumnConfig ("prescSourceName", "处方来源", 80);
        configs.add (column);
        return new ColumnModel (configs);
    }

    /**
     * 添加结果
     * 
     * @param result
     */
    public void addResult (List <PdsSupplyProModel> result)
    {
        m_grid.getListStore ().removeAll ();
        m_grid.getListStore ().add (result);
    }

    /**
     * 设置是否审核Renderer
     * 
     * @return
     */
    private GridCellRenderer <ModelData> createNeedAuditRenderer ()
    {

        return new GridCellRenderer <ModelData> ()
        {

            @Override
            public Object render (ModelData model, String property, ColumnData config, int rowIndex, int colIndex, ListStore <ModelData> store, Grid <ModelData> grid)
            {
                String checkFlag = model.get ("checkFlag");
                if (null == checkFlag)
                {
                    return getColorString (CheckFlag.UnCheck.getEnumName (), "red");
                }

                if (CheckFlag.UnCheck.getEnumCode ().equals (checkFlag))
                {
                    return getColorString (CheckFlag.UnCheck.getEnumName (), "red");
                }

                return getColorString (CheckFlag.Check.getEnumName (), "red");
            }
        };
    }

    /**
     * 创建计价状态Renderer
     * 
     * @return
     */
    private GridCellRenderer <ModelData> createChargeIndicatorRenderer ()
    {
        return new GridCellRenderer <ModelData> ()
        {

            @Override
            public Object render (ModelData model, String property, ColumnData config, int rowIndex, int colIndex, ListStore <ModelData> store, Grid <ModelData> grid)
            {
                if (null == model.get ("rcptNo"))
                {
                    return getColorString ("未记账", "red");
                }
                return getColorString ("已记账", "red");
            }
        };
    }

    /**
     * 创建处方供药状态Renderer
     * 
     * @return
     */
    private GridCellRenderer <ModelData> createPrescSupplyDrugStateRenderer ()
    {
        return new GridCellRenderer <ModelData> ()
        {

            @Override
            public Object render (ModelData model, String property, ColumnData config, int rowIndex, int colIndex, ListStore <ModelData> store, Grid <ModelData> grid)
            {
                if (null == model.get ("prescStatusName"))
                {
                    return getColorString (CodeConstants.VOID, "red");
                }
                return getColorString (model.get ("prescStatusName"), "red");
            }
        };
    }

    /**
     * 设置审核结果Renderer
     * 
     * @return
     */
    private GridCellRenderer <ModelData> createQualifiedIndicatorRenderer ()
    {
        return new GridCellRenderer <ModelData> ()
        {

            @Override
            public Object render (ModelData model, String property, ColumnData config, int rowIndex, int colIndex, ListStore <ModelData> store, Grid <ModelData> grid)
            {
                String result = model.get ("qualifiedType");
                if (null == result)
                {
                    return getColorString (CodeConstants.VOID, "red");
                }

                return getColorString (model.get ("qualifiedTypeName"), "red");
            }
        };
    }

    /**
     * 加上颜色
     * 
     * @param str
     * @return
     */
    private String getColorString (Object str, String color)
    {
        return "<span style='color:" + color + ";'>" + str + "</span>";
    }

    /**
     * 设置退药状态Renderer
     * 
     * @return
     */
    private GridCellRenderer <ModelData> createWithdrawalDrugStateRenderer ()
    {
        return new GridCellRenderer <ModelData> ()
        {

            @Override
            public Object render (ModelData model, String property, ColumnData config, int rowIndex, int colIndex, ListStore <ModelData> store, Grid <ModelData> grid)
            {
                Date result = model.get ("drugReturnTime");
                if (null == result)
                {
                    return getColorString ("未处理", "red");
                }
                return getColorString (WithdrawalDrugState.Return.getEnumName (), "red");
            }
        };
    }

    public PdsGrid <PdsSupplyProModel> getGrid ()
    {

        return m_grid;
    }

    /**
     * 清除结果
     */
    public void clear ()
    {
        m_grid.clear ();
    }

    /**
     * 点击Grid行触发此事件
     */
    public void addClickListener (Listener <GridEvent <PdsSupplyProModel>> listener)
    {
        m_grid.getGrid ().addListener (Events.RowClick, listener);
    }

    /**
     * 设置Grid第几行被选中
     * 
     * @param index 选中的行数
     */
    public void selectRow (int index)
    {
        m_grid.getGrid ().getSelectionModel ().select (index, true);
    }

    /**
     * 提交路径.
     */
    public static final String SCC_BRANCH = "$URL$";

    /**
     * 提交日期.
     */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /**
     * 最终修改人员.
     */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /**
     * 最终版本号.
     */
    public static final String SCC_REVISION = "$Revision$";
}
